const { SalesPerson } = require("../modules/salesPersonModels");
let { customer } = require('../modules/customerModules');
const jwt = require("jsonwebtoken");
const { TOKEN_KEY } = require("../utils/const");
// 获取全部的客户(销售员使用)
async function getSales(req, res) {
    const data = await SalesPerson.find();
    if (data.length > 0) {
        res.send({
            status: 1,
            message: '销售员数据请求成功',
            data
        })
    } else {
        res.send({
            status: 0,
            message: '销售员数据请求失败'
        })
    }
}
//获取销售
async function getSalesPerson(req, res) {
    const data = await SalesPerson.find({ state: "common" })
    if (data.length > 0) {
        res.send({
            status: 1,
            message: '销售员数据请求成功',
            data
        })
    } else {
        res.send({
            status: 0,
            message: '销售员数据请求失败'
        })
    }
}
//新增销售
async function addSalesPerson(req, res) {
    const { salesName, salesPassword, age, gender, power, state } = req.body;
    const data = await SalesPerson.find({ salesName, salesPassword, gender });
    if (data.length == 0) {
        SalesPerson.create({ salesName, salesPassword, age, gender, power, state });
        res.send({
            status: 1,
            message: '销售员添加成功',
            data
        })
    } else {
        res.send({
            status: 0,
            message: '销售员添加失败',
        })
    }
};
//删除
async function deleteSalesPerson(req, res) {
    const { _id } = req.body;
    const data = await SalesPerson.find({ _id });
    if (data.length > 0) {
        SalesPerson.updateMany({ _id }, { state: "down" }, function (err) { });
        res.send({
            status: 1,
            message: '销售员添加成功',
            data
        })
    } else {
        res.send({
            status: 0,
            message: '销售员添加失败',
        })
    }
};
//更新
async function updataSalesPerson(req, res) {
    const { _id, adminNme, password, age, gender, state } = req.body;
    const data = await SalesPerson.find({ _id });
    if (data.length > 0) {
        SalesPerson.updateMany({ _id }, { adminNme, password, age, gender, state }, function (err) { });
        res.send({
            status: 1,
            message: '修改成功'
        })
    } else {
        res.send({
            status: 0,
            message: '修改失败'
        })
    }
};
//获取xx销售的客户
async function getCustomer(req, res) {
    const { _id } = req.body;
    const data = await customer.find({ salesmanid: _id });
    if (data.length > 0) {
        res.send({
            status: 1,
            message: '销售员数据请求成功',
            data
        })
    } else {
        res.send({
            status: 0,
            message: '销售员数据请求失败'
        })
    }
}
module.exports = { getSalesPerson, getSales, addSalesPerson, deleteSalesPerson, updataSalesPerson, getCustomer };